def readFile(fname):
    ifile = open(fname, "r")
    lines = ifile.readlines()
    ifile.close()
    
    xx = []
    tt = []
    
    count = 0
    title = ""
    for line in lines:
        if count > 0:
            aline = line.split()
            xx.append(float(aline[0]))
            tt.append(float(aline[1]))
        else:
            title = line
        count = count + 1
    
    return xx, tt, title
    
def writeFile(fname, xx, tt, title=""):
    ofile = open(fname, "w")
    if title:
        ofile.write(title)
    for i in range(len(xx)):
        line = "%g %g\n" % (xx[i], tt[i])
        ofile.write(line)
    ofile.close()
    
if __name__ == "__main__":
    fname = "re3_front_xt.dat"
    xx, tt, title = readFile(fname)
    
    n = len(tt)
    xxt = [0] * n
    
    for i in range(1, n - 1):
        xxt[i] = 0.5 * ((xx[i + 1] - xx[i]) / (tt[i + 1] - tt[i]) + (xx[i] - xx[i - 1]) / (tt[i] - tt[i - 1]))
        
    xxt[0] = xxt[1]
    xxt[n-1] = xxt[n-2]
    
    writeFile("re3_front_xxt.dat", xxt, tt, title)
